﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Model;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;

public partial class Administration_NewItem : System.Web.UI.Page
{
    protected Categories current;

    protected void Page_Load( object sender, EventArgs e )
    {
        if( !IsPostBack )
        {
            if( !string.IsNullOrEmpty( Request.QueryString["ID"] ) )
            {
                current = Entities.GetCategoryDetails( Convert.ToInt32( Request.QueryString["ID"] ) );
                litNav.Text = LoadNavigation( current );
                ViewState["CategoryID"] = Convert.ToInt32( Request.QueryString["ID"] );
                calDatePosted.SelectedDate = DateTime.Now;
                trParent.Visible = false;
            }
            else
            {
                trParent.Visible = true;
                // parent category dropdown binder
                DataTable dt = Entities.GetTreeCategories();
                ddlParentCateg.Items.Clear();
                int level = -1;
                RecursiveFillTree( dt, 0, level );

                litNav.Text = LoadNavigation( null );
            }
            ContentPlaceHolder RightContentHolder = (ContentPlaceHolder)Master.FindControl( "RightContentHolder" );
            RightContentHolder.Visible = false;
        }
    }
    private void RecursiveFillTree( DataTable dtParent, int parentID, int level )
    {
        level++; //on the each call level increment 1
        System.Text.StringBuilder appender = new System.Text.StringBuilder();

        for( int j = 0; j < level; j++ )
        {
            appender.Append( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|" );
        }
        if( level > 0 )
        {
            appender.Append( "_ " );
        }

        DataView dv = new DataView( dtParent );

        dv.RowFilter = string.Format( "pid = {0}", parentID );
        dv.Sort = "title asc";
        int i;

        if( dv.Count > 0 )
        {
            for( i = 0; i < dv.Count; i++ )
            {
                ddlParentCateg.Items.Add( new ListItem( Server.HtmlDecode( appender.ToString() + dv[i]["Title"].ToString() ), dv[i]["id"].ToString() ) );
                RecursiveFillTree( dtParent, int.Parse( dv[i]["id"].ToString() ), level );
            }
        }
        level--; //on the each function end level will decrement by 1
    }
    protected string LoadNavigation( Categories categ )
    {
        StringBuilder sb = new StringBuilder();

        if( categ != null )
        {
            Categories current = categ;
            ArrayList nav = new ArrayList();
            nav.Add( current );
            while( current.Parent.CategoryID != 0 )
            {
                try
                {
                    current = current.Parent;
                    nav.Add( current );
                }
                catch
                {
                    break;
                }
            }
            sb.Append( @" » <a href=""Categories.aspx"">Editare categorii</a>" );

            for( int i = nav.Count - 1; i >= 0; i-- )
            {
                Categories x = (Categories)nav[i];
                sb.Append( @" » <a href=""categories.aspx?ID=" + x.CategoryID + @""">" + x.Title + @"</a>" );

            }
        }
        sb.Append( @" » Adaugare articol nou" );
        return sb.ToString();
    }
    protected void chIsFeatured_CheckedChanged( object sender, EventArgs e )
    {
        trFeaturedImage.Visible = chIsFeatured.Checked;
    }
}
